package com.uw.tookit;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Typeface;

/**
 * This class represents a pin overlay on the map. It adds a pin at the specific geo-location on the map.
 */
public class PinOverlay extends Overlay{
	private GeoPoint p;
	private String business_name = "";
	private int image;
	
	private final float M_TEXT_SIZE = 20;
	private final float M_PIN_SIZE = 50;
	
	Context context;
	
	/**
	 * Constructs a new pin overlay at the given geo-location.
	 * @param context information about an application environment.
	 * @param p Coordinates of the location to be marked.
	 * @param image image of pin to show as marker.
	 */
	public PinOverlay(Context context, GeoPoint p, int image, String name){
		this.p = p;
		this.context = context;
		this.image = image;
		this.business_name = name;
	}
	
	/**
     * Draw the pin on the canvas in the given MapView. 
     * @param canvas The Canvas upon which to draw.
     * @param mapView The MapView that requested the draw.
     * @param shadow If true, draw the shadow layer. If false, draw the overlay contents.
     * @param when The time stamp of the draw. 
     * @return True if you need to be drawn again right away; false otherwise.
     */
	@Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow){
        super.draw(canvas, mapView, shadow);
 
        //---translate the GeoPoint to screen pixels---
        Point screenPts = new Point();
        mapView.getProjection().toPixels(p, screenPts);
 
        //---add the marker---
        Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), image);
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y - M_PIN_SIZE, null);
        
        //---add the label---
        //background
        Paint backgroundPaint = new Paint();
        backgroundPaint.setARGB(255, 255, 255, 255);
        backgroundPaint.setStyle(Paint.Style.FILL);
        //border
        Paint strokePaint = new Paint();
        strokePaint.setARGB(255, 75, 75, 75);
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setStrokeWidth(4);
        //text
        Paint textPaint = new Paint();
        textPaint.setARGB(255, 0, 0, 0);
        textPaint.setTypeface(Typeface.DEFAULT_BOLD);
        textPaint.setTextSize(M_TEXT_SIZE);
        
        canvas.drawRect(screenPts.x, screenPts.y -M_PIN_SIZE- M_TEXT_SIZE, screenPts.x + textPaint.measureText(business_name), screenPts.y-M_PIN_SIZE, strokePaint);
        canvas.drawRect(screenPts.x, screenPts.y -M_PIN_SIZE- M_TEXT_SIZE, screenPts.x + textPaint.measureText(business_name), screenPts.y-M_PIN_SIZE, backgroundPaint);
        canvas.drawText(business_name, screenPts.x, screenPts.y - M_PIN_SIZE, textPaint);
	}
}
